Skip to content

Conversation

frostming
Copy link

@frostming frostming commented Oct 11, 2025

Before change:

OSError: Cannot open process memory map file '/proc/5555/maps' for PID 5555 section search: No such file or directory

During handling of the above exception, another exception occurred:

RuntimeError: Failed to find the PyRuntime section in process 5555 on Linux platform

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/frost/workspace/cpython/Lib/runpy.py", line 198, in _run_module_as_main
    return _run_code(code, main_globals, None,
                     "__main__", mod_spec)
  File "/home/frost/workspace/cpython/Lib/runpy.py", line 88, in _run_code
    exec(code, run_globals)
    ~~~~^^^^^^^^^^^^^^^^^^^
  File "/home/frost/workspace/cpython/Lib/pdb.py", line 3650, in <module>
    pdb.main()
    ~~~~~~~~^^
  File "/home/frost/workspace/cpython/Lib/pdb.py", line 3579, in main
    attach(opts.pid, opts.commands)
    ~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/frost/workspace/cpython/Lib/pdb.py", line 3448, in attach
    sys.remote_exec(pid, connect_script.name)
    ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: PyRuntime address lookup failed during debug offsets initialization

After change:

Error attaching to process: Cannot open process memory map file '/proc/5555/maps' for PID 5555 section search: No such file or directory

Close gh-139940

@bedevere-app
Copy link

bedevere-app bot commented Oct 11, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@gaogaotiantian
Copy link
Member

I think attaching to a non-exist process is a common case where we should produce better error messages. I have a feeling that the message should be even more explicit about "you have the wrong process id". @pablogsal and @godlygeek what do you think?

@godlygeek
Copy link
Contributor

Sounds reasonable to me. Maybe we want to catch the RuntimeError on POSIX and handle it by attempting os.kill(pid, 0) and seeing if that fails as well. If it does, we could raise an exception saying "Either there is no process with pid {pid} or you do not have privileges to send signals to it". That wouldn't be reasonable to do in the sys.remote_exec implementation itself, since there's no requirement there that you be able to send signals to the process, but PDB assumes you'll be able to for Ctrl-C to work, so it'd be a reasonable thing to check at this layer.

Lib/pdb.py Outdated
except RuntimeError as e:
while e.__context__ is not None:
e = e.__context__
print(f"Error attaching to process: {e}")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to get e here? We have opts.pid here to inform the user. I don't think the user understands this. I think the proper way here is to figure out what are the possibilities for RuntimeError. We should just let the user know that this failed, and the highest possibility is that the process does not exist.

Copy link
Author

@frostming frostming Oct 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this is just borrowed from

def _get_awaited_by_tasks(pid: int) -> list:
try:
return get_all_awaited_by(pid)
except RuntimeError as e:
while e.__context__ is not None:
e = e.__context__
print(f"Error retrieving tasks: {e}")
sys.exit(1)
except PermissionError as e:
exit_with_permission_help_text()
to make them symmetric.

The idea here is to hide the traceback as it may contain 3+ frames that users don't know how to handle, and at the same time, don't arbitrarily cover up any fatal errors. And only the top-most frame is the real cause.

But I am okay to change to a better way.

Lib/pdb.py Outdated
parser.error("argument -m: not allowed with argument --pid")
try:
attach(opts.pid, opts.commands)
except RuntimeError as e:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
except RuntimeError as e:
except RuntimeError as exc:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unfriendly traceback when using remote pdb to attach to a non-existing process

4 participants